home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2.sit
/
Raven 1.2
/
Source
/
Foundation
/
Common
/
ZBestFitAllocator.cpp
< prev
next >
Wrap
Text File
|
1997-07-30
|
5KB
|
207 lines
/*
* File: ZBestFitAllocator.cpp
* Summary: An allocator that uses the ODMemMgr from MacApp.
* Written by: Jesse Jones
*
* Copyright ゥ 1997 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Change History (most recent first):
*
* <2> 7/30/97 JDJ TBestFitAllocator::ValidateHeap ASSERTs that hook
* is not nil.
* <1> 1/30/97 JDJ Created
*/
#include <ZBestFitAllocator.h>
#include <BestFitH.h>
#include <ZUnitTest.h>
//-----------------------------------
// Forward References
//
class BestFitHeap;
//-----------------------------------
// Static Globals
//
static BlockValidateHook sHook = nil;
//---------------------------------------------------------------
//
// ValidateHeap
//
//---------------------------------------------------------------
#if DEBUG
static Boolean ValidateHeap( const void* block, unsigned long size, Boolean isObject, void *refCon)
{
#pragma unused(isObject)
ASSERT(sHook != nil);
(*sHook)(block, (long) (size - PrivBestFitBlock::kBusyOverhead), refCon);
return true;
}
#endif
#pragma mark -
// ===================================================================================
// class TBestFitAllocator
// ===================================================================================
//---------------------------------------------------------------
//
// TBestFitAllocator::~TBestFitAllocator
//
//---------------------------------------------------------------
TBestFitAllocator::~TBestFitAllocator()
{
delete mHeap;
}
//---------------------------------------------------------------
//
// TBestFitAllocator::TBestFitAllocator
//
//---------------------------------------------------------------
TBestFitAllocator::TBestFitAllocator(ulong initialSize, ulong poolSize, ulong hugeSize, MMHeapLocation heap)
{
mHeap = new (kMMAppMemory) BestFitHeap(initialSize, poolSize, hugeSize, heap);
mHeap->IBestFitHeap();
}
//---------------------------------------------------------------
//
// TBestFitAllocator::Allocate
//
//---------------------------------------------------------------
void* TBestFitAllocator::Allocate(ulong bytes)
{
return mHeap->Allocate(bytes);
}
//---------------------------------------------------------------
//
// TBestFitAllocator::Deallocate
//
//---------------------------------------------------------------
void TBestFitAllocator::Deallocate(void* block)
{
if (block != nil)
mHeap->Free(block);
}
//---------------------------------------------------------------
//
// TBestFitAllocator::GetHeapSize
//
//---------------------------------------------------------------
ulong TBestFitAllocator::GetHeapSize() const
{
return mHeap->HeapSize();
}
//---------------------------------------------------------------
//
// TBestFitAllocator::GetPoolCount
//
//---------------------------------------------------------------
ulong TBestFitAllocator::GetPoolCount() const
{
return mHeap->GetSegmentCount() - 1UL;
}
//---------------------------------------------------------------
//
// TBestFitAllocator::GetBlockSize
//
//---------------------------------------------------------------
ulong TBestFitAllocator::GetBlockSize(const void* ptr) const
{
return mHeap->BlockSize(ptr);
}
//---------------------------------------------------------------
//
// TBestFitAllocator::GetTotalBlockSize
//
//---------------------------------------------------------------
ulong TBestFitAllocator::GetTotalBlockSize(const void* ptr) const
{
return mHeap->BlockSize(ptr) + PrivBestFitBlock::kBusyOverhead;
}
//---------------------------------------------------------------
//
// TBestFitAllocator::ValidateBlock
//
//---------------------------------------------------------------
#if DEBUG
void TBestFitAllocator::ValidateBlock(const void* ptr) const
{
ASSERT(ptr != nil);
ASSERT(mHeap->IsValidBlock(ptr));
}
#endif
//---------------------------------------------------------------
//
// TBestFitAllocator::ValidateHeap
//
//---------------------------------------------------------------
#if DEBUG
void TBestFitAllocator::ValidateHeap(BlockValidateHook hook, void* refCon) const
{
ASSERT(sHook == nil);
ASSERT(hook != nil);
sHook = hook;
mHeap->Check(::ValidateHeap, refCon);
sHook = nil;
}
#endif
#pragma mark -
// ===================================================================================
// Unit Test
// ===================================================================================
//---------------------------------------------------------------
//
// TesTBestFitAllocator
//
// See TAllocator::TestAllocator for allocator comparisons.
//
//---------------------------------------------------------------
#if DEBUG
static void TesTBestFitAllocator()
{
TBestFitAllocator heap1(64*1024L, 32*1024L, 16*1024);
TBestFitAllocator heap2(64*1024L, 32*1024L, 16*1024);
TBestFitAllocator heap3(64*1024L, 32*1024L, 16*1024);
TAllocator::TestAllocator(heap1, heap2, heap3);
}
static TUnitTestRegistrar sBestFitAllocatorReg("BestFit Allocator", TesTBestFitAllocator);
#endif // DEBUG